home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d13 / nuf210.arc / COPY.C next >
Text File  |  1990-06-14  |  4KB  |  156 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <alloc.h>
  5. #include <conio.h>
  6. #include <ctype.h>
  7. #include <dos.h>
  8. #include <io.h>
  9. #include <fcntl.h>
  10. #include <sys\stat.h>
  11.  
  12. #include "nufind.h"
  13. #include "queue.h"
  14.  
  15. void            ErrorMsg(char *Format,...);
  16. void            ExceptionMsg(char *Format,...);
  17. int             DoCopy(char *Src, char *Dest);
  18. int             GetYorN(char *Message,...);
  19. int             MakeDir(char *Name);
  20. int                CheckPath (QUE_DEF *Q, char *Name);
  21.  
  22.  int
  23. Copy (INFO_BLOCK *Name) {
  24.     extern int      TreeSwt, CopySwt;
  25.     extern int      BaseLength;
  26.     extern char     DestDir[];
  27.     extern QUE_DEF    NoCreateQ;
  28.  
  29.     char           *p;
  30.     char            Dest[65];
  31.  
  32.     if ((Name->Attrib & (FA_DIREC | FA_LABEL)) != 0) return (1);
  33.     strcpy(Dest, DestDir);
  34.     if (Dest[strlen(Dest) - 1] != '\\') strcat(Dest, "\\");
  35.     if (TreeSwt) strcat(Dest, &Name->Name[BaseLength]);
  36.     else {
  37.         p = strrchr(Name->Name, '\\');
  38.         strcat(Dest, p + 1);
  39.         }
  40.     if (!strcmp(Name->Name, Dest)) return (0);
  41.     if (NoCreateQ.Count != 0)
  42.         if ( CheckPath(&NoCreateQ, Dest) != 0 ) return(0);
  43.     printf("%s: %s TO %s\n", (CopySwt) ? "COPY" : "MOVE", Name->Name, Dest);
  44.     if (!access(Dest, 0))
  45.         if (!GetYorN("Destination file exists: Overwrite ")) return (1);
  46.     return( DoCopy(Name->Name, Dest) );
  47.     }
  48.  
  49.  int
  50. Move (INFO_BLOCK *Name) {
  51.     extern char     DestDir[];
  52.  
  53.     char           *p;
  54.     char            Dest[65];
  55.     int             Result;
  56.  
  57.     if ((Name->Attrib & (FA_DIREC | FA_LABEL)) != 0) return (0);
  58.     if (Name->Name[0] != DestDir[0]) {
  59.         if (!Copy(Name)) unlink(Name->Name);
  60.         return (0);
  61.         }
  62.     strcpy(Dest, DestDir);
  63.     if (Dest[strlen(Dest) - 1] != '\\') strcat(Dest, "\\");
  64.     if (TreeSwt) strcat(Dest, &Name->Name[BaseLength]);
  65.     else {
  66.         p = strrchr(Name->Name, '\\');
  67.         strcat(Dest, p + 1);
  68.         }
  69.     printf("MOVE: %s TO %s\n", Name->Name, Dest);
  70.     if (!access(Dest, 0)) {
  71.         if (!GetYorN("Destination file exists: Overwrite ")) return (1);
  72.         unlink(Dest);
  73.         }
  74.     if ( (Result = MakeDir(Dest)) != 0 ) {
  75.         if (Result == 2) return(-1);
  76.         ExceptionMsg("Cannot create destination directory: %s", Dest);
  77.         return (-1);
  78.         }
  79.     if ((Result = rename(Name->Name, Dest)) != 0)
  80.         ExceptionMsg("FAILED");
  81.     return (Result);
  82.     }
  83.  
  84.  
  85.  int
  86. DoCopy (char *Src, char *Dest) {
  87.     extern          errno;
  88.     extern int        OldDate, CopySwt;
  89.  
  90.     unsigned        BuffSize;
  91.     char           *BuffPtr;
  92.     int             Sf, Df;
  93.     int             InCount, OutCount;
  94.     int                Result;
  95.     struct ftime    DateTime;
  96.  
  97.     if (coreleft() > 32 * 1024 - 1) BuffSize = 32 * 1024 - 1;
  98.     else if ( (BuffSize = (unsigned) (coreleft() - 100) ) < 512) {
  99.         ErrorMsg("Insufficient memory for COPY buffer!\n");
  100.         return (-1);
  101.         }
  102.  
  103.     if ((BuffPtr = malloc(BuffSize)) == NULL) {
  104.         ErrorMsg("Insufficient memory for COPY buffer!\n");
  105.         return (-1);
  106.         }
  107.  
  108.     if ((Sf = open(Src, O_RDONLY | O_BINARY)) == 0) {
  109.         ExceptionMsg("Open error on source file: %s", Src);
  110.         free(BuffPtr);
  111.         }
  112.     if ( (Result = MakeDir(Dest)) != 0 ) {
  113.         if (Result != 2)
  114.             ExceptionMsg("Cannot create destination directory: %s", Dest);
  115.         close(Sf);
  116.         free(BuffPtr);
  117.         return (-1);
  118.         }
  119.     if ((Df = open(Dest, O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE)) < 0) {
  120.         ExceptionMsg("Open error on destination file: %s", Dest);
  121.         close(Sf);
  122.         free(BuffPtr);
  123.         return (-1);
  124.         }
  125.  
  126.     while ((InCount = read(Sf, BuffPtr, BuffSize)) > 0) {
  127.         OutCount = write(Df, BuffPtr, InCount);
  128.         if (OutCount < 0 || OutCount != InCount) {
  129.             ExceptionMsg("Write error on %s ", Dest);
  130.             close(Sf);
  131.             close(Df);
  132.             unlink(Dest);
  133.             free(BuffPtr);
  134.             return (-1);
  135.             }
  136.         }
  137.  
  138.     if (InCount < 0) {
  139.         ExceptionMsg("Read error on %s", Src);
  140.         close(Sf);
  141.         close(Df);
  142.         unlink(Dest);
  143.         free(BuffPtr);
  144.         return (-1);
  145.         }
  146.  
  147.     if (OldDate || !CopySwt) {
  148.         getftime(Sf, &DateTime);
  149.         setftime(Df, &DateTime);
  150.         }
  151.     close(Sf);
  152.     close(Df);
  153.     free(BuffPtr);
  154.     return (0);
  155.     }
  156.